<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_stpinggou/model')->方法名();
*/
namespace jy_stpinggou;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function current_level_up_types(){
		return str_array($this->fenxiaoset['level_up_types']);
	}
	/**
     * 导入微豆
     */
	public function up_credit4($rows){
		foreach($rows as $item){
			if(!$item['fee'] || $item['fee']<=0 || !$item['mobile']){
				continue;
			}
			$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'mobile'=>$item['mobile']));
			model('core/member')->member_credit($member['id'],'credit4',$item['fee'],true,'jy_stpinggou','批量导入');
		}
		return true;
	}
	public function get_csvitems($rows){
			$num = count($rows);
			$i = 0;
			$colsIndex = $rows[1];
			$rows = array_slice($rows, 2, count($rows) - 2);
			$items = array();
			$num = 0;
			foreach ($rows as $rownu => $rv) {
				$item = array();
				foreach ($colsIndex as $cols => $cv) {
						$item[$cv]=$rv[$cols];
				}
				$items[] = $item;
				++$num;
			}
			return $items;
	}
	/*
		刷选团ID
	*/
	public function checkteam($goodsid,$teamid=0){
		$goods = pdo_get('jy_stpinggou_goods',array('goodsid' => $goodsid,'uniacid' => $_SESSION['uniacid']));
		$group = pdo_get('jy_stpinggou_group',array('id' =>$goods['groupid'],'uniacid' => $_SESSION['uniacid']));
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		if($_SESSION['uid']){
			$isgroupnum=pdo_count('jy_stpinggou_log',array('mid' => $_SESSION['uid'],'createtime >'=>$daytime,'status'=>1,'type'=>0,'groupid'=>$goods['groupid'],'uniacid'=>$_SESSION['uniacid']),'id');
		}
		if($group['pingtotal']&& $isgroupnum >= $group['pingtotal']){
			exi('您已参与'.$group['pingtotal'].'次，无法参与该区域的拼团！','error','jy_stpinggou/index/');
		}
		if ($teamid){
			$team = pdo_get('jy_weishop_groups_team',array('id'=>$teamid,'uniacid' => $_SESSION['uniacid']));
			if ($team['status']){
				$teamid=$this->getteamid($goodsid);
				//exi('该团已完成或过期，请参与其他拼单！','error','jy_stpinggou/index/');
			}
			$isnum=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_stpinggou','status'=>array(1,2,3,5,6,10),'teamid'=>$teamid));
			if ($goods['teamnum']<=$isnum){
				$teamid=$this->getteamid($goodsid);
				//exi('该团已完成,不能再参与！','error','jy_stpinggou/index/');
			}
		}
		return $teamid;
	}
	/*
		拼团失败
	*/
	public function order_cancel($teamid){
		$params=array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_stpinggou','teamid'=>$teamid,'status'=>10);
		$order=pdo_getall('jy_weishop_order',$params,array('id','mid','teamid','price','paytype'));
		if($this->plugin_exists('jy_stteamfee')){
			$jy_stteamfee=true;
		}
		if($this->plugin_exists('jy_area_agent')){
			$jy_area_agent=true;
		}
		foreach($order as $item){
			$this->order_choufail($item);
			if($jy_stteamfee){
				pdo_update('jy_stteamfee_commission',array('status'=>3,'complete_time'=>TIMESTAMP),array('status'=>0,'ordersn'=>$item['ordersn']));
			}
			if($jy_area_agent){
				pdo_update('jy_area_agent_commission',array('status'=>3,'complete_time'=>TIMESTAMP),array('status'=>0,'ordersn'=>$item['ordersn']));
			}
		}
		pdo_update('jy_weishop_groups_team',array('status'=>2,'uptime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'id'=>$teamid));
	}
	/*
		拼团成功
	*/
	public function order_success($teamid){
		$data=array(
			'status'=>1,
		);
		$goodsid=pdo_getcolumn('jy_weishop_groups_team',array('uniacid'=>$_SESSION['uniacid'],'id'=>$teamid),'goodsid');
		$orders=pdo_getall('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_stpinggou','teamid'=>$teamid,'status'=>10));
		//抽中
		$pin_goods=pdo_get('jy_stpinggou_goods',array('goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
		if($this->plugin_exists('jy_stteamfee')){
			$jy_stteamfee=true;
		}
		if($this->plugin_exists('jy_area_agent')){
			$jy_area_agent=true;
		}
		foreach($orders as $key=>$ov){
			$this->model('renwu')->canfen($ov,$pin_goods);
			if($jy_stteamfee){
				pdo_update('jy_stteamfee_commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('status'=>0,'ordersn'=>$ov['ordersn']));
			}
			if($jy_area_agent){
				pdo_update('jy_area_agent_commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('status'=>0,'ordersn'=>$ov['ordersn']));
			}
			//看还可以中不？
			$canzhong=$this->model('model')->check_canzhong($pin_goods['id'],$ov['mid']);
			if(!$canzhong){
				//不可以中的
				$ov['goodsid']=$goodsid;
				$this->iszhong_get($ov,$pin_goods);
				unset($orders[$key]);
			}
		}
		//可以中的：
		$this->orders=array_values($orders);
		$num=count($this->orders);
		$zhongorders=array();
		for($i=1;$i<=$pin_goods['lastnum'];$i++){
			$key=mt_rand(0,$num-$i);
			if(!$this->orders[$key]['id']){
				$key=0;
			}
			pdo_update('jy_weishop_order',$data,array('id'=>$this->orders[$key]['id']));
			$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid));
			$this->orders[$key]['finishtime']=TIMESTAMP;
			$this->orders[$key]['goodstitle']=$goods['title'];
			$this->orders[$key]['orderprice']=$this->orders[$key]['price'];			
			$params=array(
				'pingid'=>$pin_goods['id'],
				'groupid'=>$pin_goods['groupid'],
				'total_price'=>$this->orders[$key]['price'],
				'msg'=>'拼团成功！',
				'status'=>1,
				'mid'=>$this->orders[$key]['mid'],
			);
			$this->model('model')->set_log($params,3);
			$zhongorders[]=$this->orders[$key];
			unset($this->orders[$key]);
			$this->orders=array_values($this->orders);
		}
		//可以中，但没中的
		foreach($this->orders as $ov){
			$ov['goodsid']=$goodsid;
			$this->order_fanxian($ov);
			$this->order_choufail($ov);
		}
		pdo_update('jy_weishop_groups_team',array('status'=>1,'uptime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'id'=>$teamid));
		//中了的
		foreach($zhongorders as $zv){
			$zv['goodsid']=$goodsid;
			$this->order_fahuo($zv);
			$this->order_zhongfanxian($zv);
			if($this->plugin_exists('jy_stteamfee')){
				model('jy_stteamfee/commission')->pzfee($zv);
			}
			model('jy_weishop/notice')->notice_groupfinish($zv);
			$this->publish('order.change',$zv);
		}
		return true;
	}
	//退款
	public function order_choufail($item){
			//$check=model('jy_weishop/order/refund')->check_order_rerefund_pin(intval($item['id']));
			//if(!$check){
			//	return false;
			//}
			$params=array(
				'orderid'=>intval($item['id']),
				'images'=>$item['images']?ijson_encode($item['images']):'',
				'price'=>$item['price'],
				'plugin_name'=>'jy_stpinggou',
				'content'=>'拼单失败退款',
			);
			$refund_id=model('jy_weishop/order/refund')->refund_create($params,true);
			if($this->fenxiaoset['is_zdrefund']==1 && $refund_id){
				model('jy_weishop/order/refund')->refund_pay($refund_id,true,true,'credit');
				model('jy_weishop/notice')->notice_groupfail($item);
			}
			return true;
	}
	/*
		失败奖励
	*/
	public function order_fanxian($order,$pluginset=array()){
		//$pluginset=$pluginset?:$this->plugin_setting('basic');
		$teamgoods=pdo_get('jy_stpinggou_goods',array('goodsid'=>$order['goodsid'],'uniacid'=>$_SESSION['uniacid']));
		$fan_type=$teamgoods['fan_type']?:1;
		$fanfee=$teamgoods['fan_fee'];
		$feelog=array(
			'mid'=>$order['mid'],
			'msg'=>'拼团未中奖励',
			'gift_type'=>$fan_type,
			'status'=>1,
		);
		if($fanfee>0 && $fan_type!=9){
			$feelog['fee']=$fanfee;
			$this->set_feelog($feelog,5);
		}
		if($fan_type==9){
			$fanfee1=$teamgoods['fan_fee1'];
			$fanfee2=$teamgoods['fan_fee2'];
			if($fanfee1>0){
				$feelog['gift_type']=1;
				$feelog['fee']=$fanfee1;
				$this->set_feelog($feelog,5);
			}
			if($fanfee2>0){
				$feelog['gift_type']=2;
				$feelog['fee']=$fanfee2;
				$this->set_feelog($feelog,5);
			}
			$fanfee=$fanfee1;
		}
		if($fanfee>0){
			model('core/member')->member_credit($order['mid'],'credit'.$fan_type,$fanfee,true,'jy_stpinggou','拼团未中奖励');
			model('jy_weishop/notice')->notice_groupfinish_fan($order,$fanfee,'拼单参与奖励');
		}
		return true;
	}
	/*
		拼中奖励
	*/
	public function order_zhongfanxian($order,$pluginset=array()){
		//$pluginset=$pluginset?:$this->plugin_setting('basic');
		$teamgoods=pdo_get('jy_stpinggou_goods',array('goodsid'=>$order['goodsid'],'uniacid'=>$_SESSION['uniacid']));
		$fan_type=1;
		$fanfee=$teamgoods['zhongfan_fee'];
		if($fanfee>0){
			model('core/member')->member_credit($order['mid'],'credit'.$fan_type,$fanfee,true,'jy_stpinggou','拼中奖励积分');
			model('jy_weishop/notice')->notice_groupfinish_fan($order,$fanfee,'拼中奖励积分');
		}
		return true;
	}
	/*
		区域拼中送拼团币
	*/
	public function group_pingbfee($order,$groupid=0){
		return false;
		$group=pdo_get('jy_stpinggou_group',array('id'=>$groupid,'uniacid'=>$_SESSION['uniacid']));
		if(!$group['pingbfee'] || $group['pingbfee']<=0){
			return false;
		}
		$feelog=array(
				'groupid'=>$groupid,
				'mid'=>$order['mid'],
				'msg'=>'拼中送拼团金',
				'status'=>1,
				'fee'=>$group['pingbfee'],
		);
		$this->model('model')->set_feelog($feelog,3);
		$isping=pdo_count('jy_stpinggou_feelog',array('type'=>3,'mid'=>$order['mid'],'status'=>1,'groupid'=>$groupid,'uniacid'=>$_SESSION['uniacid']));	
		$allfee=pdo_getsum('jy_stpinggou_feelog',array('type'=>3,'mid'=>$order['mid'],'status'=>1,'groupid'=>$groupid,'uniacid'=>$_SESSION['uniacid']),'fee');
		$fromuser=pdo_getcolumn('jy_stpinggou_member',array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
		if($fromuser){
			pdo_update('jy_stpinggou_member',array('team_pingfee +='=>$group['pingbfee']),array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
			$this->get_red_jiafee($order,$fromuser);
		}
		if($isping>=intval($group['pingbnum']) && $allfee>0){
			$feelog=array(
				'groupid'=>$groupid,
				'mid'=>$order['mid'],
				'msg'=>'满次拼团金兑换消耗',
				'status'=>1,
				'fee'=>$allfee,
			);
			$this->model('model')->set_feelog($feelog,4);
			$this->send_pingbfee($allfee,$group,$order['mid']);
			pdo_update('jy_stpinggou_feelog',array('status'=>2),array('type'=>3,'mid'=>$order['mid'],'status'=>1,'groupid'=>$groupid,'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
	/*
		拼团金兑换
	*/
	public function send_pingbfee($allfee,$group=array(),$mid=0){
		if($group['dui_jifen1']){
			$jifen1=$group['dui_jifen1']*$allfee/100;
			model('core/member')->member_credit($mid,'credit1',$jifen1,true,'jy_stpinggou','拼团金兑换成积分');
		}
		if($group['dui_jifen3']){
			$jifen3=$group['dui_jifen3']*$allfee/100;
			model('core/member')->member_credit($mid,'credit3',$jifen3,true,'jy_stpinggou','拼团金兑换成金豆');
		}
		if($group['dui_licai']){
			$baofee=$group['dui_licai']*$allfee/100;
			$this->model('member')->recharge($mid,$baofee,0,'拼团金兑换成倍财金');
		}
		return true;
	}
	/*
		获得加速金
	*/
	public function get_red_jiafee($order,$fromuser=0){
		if(!$fromuser){
			$fromuser=pdo_getcolumn('jy_stpinggou_member',array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
		}
		if(!$fromuser){
			return false;
		}
		if($this->fenxiaoset['red_pingfee']<=0 || $this->fenxiaoset['red_pingfee']<=0){
			return false;
		}
		$member=pdo_get('jy_stpinggou_member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
		if($member['team_pingfee']>=$this->fenxiaoset['red_pingfee']){
			pdo_update('jy_stpinggou_member',array('jiafee +='=>$this->fenxiaoset['red_jiafee'],'team_pingfee -='=>$this->fenxiaoset['red_pingfee']),array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
			$feelog=array(
				'groupid'=>$groupid,
				'mid'=>$order['mid'],
				'msg'=>'团队拼团金兑换加速金',
				'status'=>1,
				'fee'=>$this->fenxiaoset['red_jiafee'],
			);
			$this->model('model')->set_feelog($feelog,6);
		}
		return true;
	}
	/*
		拼中发货
	*/
	public function order_fahuo($order){
		$order['goods']=pdo_getall('jy_weishop_order_goods',array('orderid'=>$order['id']));
		foreach($order['goods'] as $vv){
				//已经整理出了商品类型type,isverify，只要支持核销，无论什么商品都可以核销！
				$goods=pdo_get('jy_weishop_goods',array('id'=>$vv['goodsid']),array('type','isverify','virtualsend','virtualsendcont','virtual_template_id'));
				if($goods['isverify']==1 && $goods['type']!=2){
					if($goods['type']==3){
						model('jy_weishop/order')->order_send_verifygoods($order['id'],$vv['goodsid'],$order['sendtype']);
					}elseif($goods['type']==0){
						//实体物品，需配送时，依然生成核销码，但是不刷新配送状态
						//model('jy_weishop/order')->order_send_verify($order['id'],$vv['goodsid'],$order['sendtype']);
					}else{
						model('jy_weishop/order')->order_send_verify($order['id'],$vv['goodsid'],$order['sendtype']);
					}
				}elseif($goods['type']==1 || $goods['type']==2){
					//虚拟发货，如果是自动发货
					if(($goods['virtualsend']) || $goods['type']==2){
						$vv=array_merge($vv,$goods);
						model('jy_weishop/order')->order_send_virtual($order['id'],$vv);
					}
				}
		}
		return true;
	}
	/*
		已经中满了，直接失败
	*/
	public function iszhong_get($order,$pinggoods=array()){
		$order['goodsid']=$pinggoods['goodsid'];
		$this->order_fanxian($order);
		$this->order_choufail($order);
		$otherzhong=array();
		$otherzhong[]=$order;
		$this->orderzhong($otherzhong,$pinggoods);
		return true;
	}
	public function up_price($goods,$pluginset=array(),$isprice=false,$isexi=false){
		$teamgoods=pdo_get('jy_stpinggou_goods',array('goodsid'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']));
		if((!$teamgoods['id'] || !$teamgoods['status']) && $isexi){
			exi('选择的商品不存在或已下架！','error','/index/');
			//先注销，如果商品状态停用的时候，会出现价格异常；
			//return $goods;
		}
		$pluginset=$pluginset?:$this->fenxiaoset;
		$goods['ding_price']=$teamgoods['ding_price'];
		if($goods['groupprice']==0){
				$pluginset['groupdiscount']=$pluginset['groupdiscount']?:98;
				$goods['groupprice']=price($goods['price']*$pluginset['groupdiscount']/100);
		}
		$goods['grouptime']=$teamgoods['timeout']?:$pluginset['timeout'];
		$goods['grouptime']=$goods['grouptime']?:120;
		$goods['teamnum']=$teamgoods['teamnum']?:$pluginset['teamnum'];
		$goods['teamnum']=$goods['teamnum']?:2;
		$goods['groupprice']=$teamgoods['price']?:$goods['groupprice'];
		$goods['fan_fee']=$teamgoods['fan_type']==9?$teamgoods['fan_fee1']:$teamgoods['fan_fee'];
		$goods['lastnum']=$teamgoods['lastnum'];
		$goods['failnum']=$teamgoods['teamnum']-$teamgoods['lastnum'];
		if($isprice){
			$goods['groupprice']=price($goods['groupprice']);
			$goods['fan_fee']=price($goods['fan_fee']);
			$goods['ding_price']=price($goods['ding_price']);
		}
		$goods['fan_price']=$goods['fan_fee'];
		$goods['fan_typetitle']=$this->credit_type[$teamgoods['fan_type']];
		return $goods;
	}
	public function up_safeprice($goods,$pluginset=array(),$isprice=false){
		$teamgoods=pdo_get('jy_stpinggou_goods',array('id'=>$goods['id'],'uniacid'=>$_SESSION['uniacid']));
		if($teamgoods['id'] && !$teamgoods['status']){
			return $goods;
			//exi('选择的拼团商品不存在或已下架！','error','/index/');
		}
		$pluginset=$pluginset?:$this->fenxiaoset;
		$goods['ding_price']=$teamgoods['ding_price'];
		if($goods['groupprice']==0){
				$pluginset['groupdiscount']=$pluginset['groupdiscount']?:98;
				$goods['groupprice']=price($goods['goodsprice']*$pluginset['groupdiscount']/100);
		}
		if($pluginset['bao_type']==1){
				$goods['ding_price']=$goods['ding_price']?:$pluginset['bao_fee'];
		}else{
				$goods['ding_price']=$goods['ding_price']?:$pluginset['bao_rate']*$goods['goodsprice']/100;
		}
		if($pluginset['bao_type']==1){
				$goods['ding_price']=$goods['ding_price']?:$pluginset['bao_fee'];
		}elseif($pluginset['bao_rate']){
				$goods['ding_price']=$goods['ding_price']?:$pluginset['bao_rate']*$goods['goodsprice']/100;
		}
		if($pluginset['groupdiscount']){
				$goods['groupprice']=$pluginset['groupdiscount']*$goods['goodsprice']/100;
		}
		$goods['grouptime']=$teamgoods['timeout']?:$pluginset['timeout'];
		$goods['grouptime']=$goods['grouptime']?:48;
		$goods['teamnum']=$teamgoods['teamnum']?:$pluginset['teamnum'];
		$goods['teamnum']=$goods['teamnum']?:2;
		$goods['groupprice']=$teamgoods['price']?:$goods['groupprice'];
		$goods['fan_fee']=$teamgoods['fan_fee'];
		$goods['lastnum']=$teamgoods['lastnum'];
		$goods['failnum']=$teamgoods['teamnum']-$teamgoods['lastnum'];
		if($isprice){
			$goods['groupprice']=price($goods['groupprice']);
			$goods['fan_fee']=price($goods['fan_fee']);
			$goods['ding_price']=price($goods['ding_price']);
		}
		$goods['fan_price']=$goods['fan_fee'];
		$goods['fan_typetitle']=$this->credit_type[$teamgoods['fan_type']];
		return $goods;
	}
	/*
		判断预约
	*/
	public function set_yuyue($pingid,$jiqiset=false,$mid=0,$addressid=0){
		$mid=$mid?:$_SESSION['uid'];
		$is_yuyue=$this->is_yuyue($pingid,$mid);
		if($is_yuyue>0){
			return false;
		}
		$member=pdo_get('jy_stpinggou_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if($member['jsjiqi']){
			$isjiqi=1;
			if(!$jiqiset){
				$canyu=pdo_get('jy_stpinggou_canyu',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'pingid'=>$pingid));
				if(!$canyu['id']){
					$insert=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mid,
						'pingid'=>$pingid,
						'isjiqi'=>$isjiqi,
						'addressid'=>$addressid,
						'createtime'=>TIMESTAMP
					);
					pdo_insert('jy_stpinggou_canyu',$insert);	
					exi('参与本拼团成功！！','error');
				}
				exi('您已购买了机器人，无需手动预约！','error');
			}
		}
		$is_yuyuefail=$this->is_yuyuefail($pingid,$mid);
		$pinggoods=pdo_get('jy_stpinggou_goods',array('id'=>$pingid,'uniacid'=>$_SESSION['uniacid']));
		$times=$this->model('model')->get_times($pinggoods['timeids'],true);
		if($member['jiqiendtime'] && $member['jiqiendtime']<TIMESTAMP){
			$isjiqi=0;
			if($jiqiset){
				$params=array(
					'pingid'=>$pingid,
					'msg'=>'预约失败！您购买机器助手已到期！',
					'status'=>0,
					'timeid'=>$times,
					'mid'=>$mid,
				);
				if(!$is_yuyuefail){
					$this->set_log($params,1);
				}
				return false;
			}
			//exi('您购买机器助手已到期！','error');
		}
		$item=$this->model('member')->getdetail($mid);
		if($pinggoods['price']>$item['baofee']){
			$params=array(
					'pingid'=>$pingid,
					'msg'=>'倍财金不足，不能预约本拼团',
					'status'=>0,
					'timeid'=>$times,
					'mid'=>$mid,
			);
			if(!$is_yuyuefail){
				$this->set_log($params,1);
			}
			if(!$jiqiset){
				exi('倍财金不足，当前倍财金（'.$item['baofee'].'元），不能预约本拼团！','error','/recharge/');
			}else{
				return false;	
			}
		}
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'pingid'=>$pingid,
			'isjiqi'=>$isjiqi,
			'addressid'=>$addressid,
			'createtime'=>TIMESTAMP
		);
		if(!$times){
			if(!$jiqiset){
				exi('还不能预约！当天没有活动了！','error');
			}else{
				return false;	
			}
		}
		$startime = strtotime(date('Y-m-d ' . $times));
		$basic=$this->plugin_setting('basic');
		$jiqi_yuyuetime=$basic['jiqi_yuyuetime']?:30;
		if(TIMESTAMP+$jiqi_yuyuetime*60<$startime){
			if(!$jiqiset){
				exi('还不能预约！只能提前：'.$jiqi_yuyuetime.'分钟预约！','error');
			}else{
				return false;	
			}
		}
		$insert['times']=$times;
		$insert['dayid']=date('Y-m-d',TIMESTAMP);
		$yuyue=pdo_get('jy_stpinggou_yuyue',array('dayid'=>$insert['dayid'],'times'=>$times,'uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'pingid'=>$pingid));
		if($yuyue['id']){
			if(!$jiqiset){
				exi('当前时段你已预约！','error');
			}else{
				return false;	
			}
		}
		pdo_insert('jy_stpinggou_yuyue',$insert);
		$params=array(
					'pingid'=>$pingid,
					'msg'=>'预约成功！',
					'status'=>1,
					'mid'=>$mid,
					'timeid'=>$times,
		);
		$this->set_log($params,1);
		$this->model('member')->recharge($mid,$pinggoods['price'],2,'预约冻结倍财金');
		return true;
	}
	/*
		已预约？
	*/
	public function is_yuyue($pingid,$mid=0,$timeid=''){
		$mid=$mid?:$_SESSION['uid'];
		if(!$timeid){
			$pinggoods=pdo_get('jy_stpinggou_goods',array('id'=>$pingid,'uniacid'=>$_SESSION['uniacid']));
			$timeid=$this->model('model')->get_times($pinggoods['timeids'],true);
		}
		$isyuyue=pdo_getcolumn('jy_stpinggou_yuyue',array('dayid'=>date('Y-m-d',TIMESTAMP),'times'=>$timeid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'pingid'=>$pingid),'id');
		return $isyuyue;
	}
	/*
		已预约失败？
	*/
	public function is_yuyuefail($pingid,$mid=0,$timeid=''){
		$mid=$mid?:$_SESSION['uid'];
		if(!$timeid){
			$pinggoods=pdo_get('jy_stpinggou_goods',array('id'=>$pingid,'uniacid'=>$_SESSION['uniacid']));
			$timeid=$this->model('model')->get_times($pinggoods['timeids'],true);
		}
		$params=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'pingid'=>$pingid,
			'timeid'=>$timeid,
			'type'=>1,
			'dayid'=>date('Y-m-d',TIMESTAMP),
		);
		$isping=pdo_get('jy_stpinggou_log',$params);
		if($isping['id']){
			return true;
		}else{
			return false;
		}
	}
	/*
		日志信息
	*/
	public function set_log($params,$type=0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid']?:$_SESSION['uid'],
			'pingid'=>$params['pingid'],
			'groupid'=>$params['groupid'],
			'msg'=>$params['msg'],
			'status'=>$params['status'],
			'gift_type'=>$params['gift_type'],
			'other_goodsid'=>$params['other_goodsid'],
			'other_credit_fee'=>$params['other_credit_fee'],
			'total_price'=>$params['total_price'],
			'type'=>$type,
			'timeid'=>$params['timeid'],
			'dayid'=>date('Y-m-d',TIMESTAMP),
			'teamid'=>$params['teamid'],
			'canzhong'=>$params['canzhong'],
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_stpinggou_log',$insert);
		return true;
	}
	/*
		fee信息0自己兑换余额，1推荐下级兑换余额
	*/
	public function set_feelog($params,$type=0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'groupid'=>$params['groupid'],
			'mid'=>$params['mid']?:$_SESSION['uid'],
			'msg'=>$params['msg'],
			'status'=>$params['status'],
			'gift_type'=>$params['gift_type'],
			'fee'=>$params['fee'],
			'type'=>$type,
			'dayid'=>date('Y-m-d',TIMESTAMP),
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_stpinggou_feelog',$insert);
		return true;
	}
	//确认当前时段是否已拼单
	public function che_isping($pingid,$mid,$timeid,$dayid='',$uniacid=0){
		$params=array(
			'uniacid'=>$uniacid?:$_SESSION['uniacid'],
			'mid'=>$mid,
			'pingid'=>$pingid,
			'timeid'=>$timeid,
			'type'=>0,
			'dayid'=>$dayid?:date('Y-m-d',TIMESTAMP),
		);
		$isping=pdo_get('jy_stpinggou_log',$params);
		if($isping['id']){
			return true;
		}else{
			return false;
		}
	}
	//获取当前拼单时间
	public function get_times($timeids,$getnext=false,$getcanping=false){
		if(is_array($timeids)){
			
		}else{
			$timeids=str_array($timeids);
		}
		$times=array();
		foreach($timeids as $k=>$time){
			$times[]=array(
				'strtotime'=>strtotime(date('Y-m-d ' . $time)),
				'time'=>$time
			);
		}
		paixu($times,'strtotime','asc');
		foreach($times as $key=>&$tv){
			$startime = strtotime(date('Y-m-d ' . $tv['time']));
			$endtv=$times[$key+1]['time'];
			if($endtv){
				$endtime = strtotime(date('Y-m-d ' . $endtv));
			}else{
				$endtime = $startime+30*60;
			}
			if($startime>TIMESTAMP){
				if($getnext){
					return $tv['time'];
				}
				$status=0;
			}elseif($startime<TIMESTAMP && $endtime<TIMESTAMP){
				$status=2;
			}else{
				$canping=1;
				$status=1;
				$timeid=$tv['time'];
			}
			$tv['endtime']=$endtv;
			$tv['status']=$status;
		}
		if($getcanping){
			return $canping;
		}elseif($getnext){
			return false;
		}else{
			return $times;
		}
	}
	//获取当前拼单时间
	public function get_timeid($timeids){
		$timeids=str_array($timeids);
		$times=array();
		foreach($timeids as $k=>$time){
			$times[]=array(
				'strtotime'=>strtotime(date('Y-m-d ' . $time)),
				'time'=>$time
			);
		}
		paixu($times,'strtotime','asc');
		$timeid='fail';
		foreach($times as $key=>$tv){
			$startime = strtotime(date('Y-m-d ' . $tv['time']));
			$endtv=$times[$key+1]['time'];
			if($endtv){
				$endtime = strtotime(date('Y-m-d ' . $endtv));
			}else{
				$endtime = $startime+30*60;
			}
			if($startime>TIMESTAMP){
				$status=0;
			}elseif($startime<TIMESTAMP && $endtime<TIMESTAMP){
				$status=2;
			}else{
				$canping=1;
				$status=1;
				$timeid=$tv['time'];
				break;
			}
		}
		return $timeid;
	}
	//确认用户
	public function check_canzhong($pingid,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$pinggoods=pdo_get('jy_stpinggou_goods',array('id'=>$pingid,'uniacid'=>$_SESSION['uniacid']));
		$group = pdo_get('jy_stpinggou_group',array('id' =>$pinggoods['groupid'],'uniacid' => $_SESSION['uniacid']));
		$pingids=pdo_getall('jy_stpinggou_goods',array('groupid'=>$groupid,'uniacid'=>$_SESSION['uniacid']),'id','id');	
		$pingids=$pingids?array_keys($pingids):-1;
		$num=$this->get_orderzhongnum($pingids,$mid);
		if($group['pingzmaxnum'] && $num>=$group['pingzmaxnum']){
			return false;
		}
		if($group['pingznum'] && $group['pingbnum']){
			$psize = $group['pingznum']+$group['pingbnum'];
			$pindex = max(1, intval($_RQ['page']));
			$listlog=pdo_getslice('jy_stpinggou_log',array('pingid'=>$pingids,'mid'=>$mid,'type'=>array(0,3),'status'=>1),array($pindex,$psize),$total,array(),'','createtime desc');
			$lstype=0;
			$isznum=0;
			foreach($listlog as $lv){
				if($lv['type']==3){
					$isznum +=1;
				}
				if($isznum>=$group['pingznum']){
					return false;
				}
			}
		}
		return true;
	}
	//确认用户命中个数
	public function get_orderzhongnum($pingids,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$num=pdo_count('jy_stpinggou_log',array('createtime >='=>$daytime,'pingid'=>$pingids,'mid'=>$mid,'type'=>3,'status'=>1));
		return $num;
	}
	//额外命中个数
	public function get_otherzhongnum($pingid,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$pindays=pdo_get('jy_stpinggou_pindays',array('mid'=>$mid,'pingid'=>$pingid,'uniacid'=>$_SESSION['uniacid']));		
		$starttime=$pindays['starttime']?:TIMESTAMP;
		$num=pdo_count('jy_stpinggou_log',array('createtime >='=>$starttime,'pingid'=>$pingid,'mid'=>$mid,'type'=>4,'status'=>1),'id');
		return $num;
	}
	/*
		额外中奖
	*/
	public function orderzhong($orders,$pin_goods=array()){
		//先注销
		return true;
		foreach($orders as $item){
			if($pin_goods['other_share']){
				$other_share=$this->get_agentnum($item['mid']);
				if($other_share<$pin_goods['other_share']){
					return false;
				}
			}
			$otherzhongnum=$this->get_otherzhongnum();
			if($otherzhongnum>0){
				return false;
			}
			$zhong=mt_rand(0,100);
			if($zhong<=$pin_goods['otherzhong']){
				$params=array(
					'pingid'=>$pin_goods['id'],
					'msg'=>'恭喜获得额外中奖！',
					'status'=>1,
					'gift_type'=>$pin_goods['other_type'],
					'other_goodsid'=>$pin_goods['other_goodsid'],
					'other_credit_fee'=>$pin_goods['other_credit_fee'],
					'mid'=>$item['mid'],
				);
				$this->set_log($params,4);
				if($pin_goods['other_type']>=1){
						model('core/member')->member_credit($item['mid'],'credit'.$pin_goods['other_type'],$pin_goods['other_credit_fee'],true,'jy_stpinggou','拼单参与额外大奖');
				}
			}
		}
		return true;
	}
	/*
		推荐N个人参与了
	*/
	public function get_agentnum($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$downmids=pdo_getall('jy_stpinggou_member',array('fromuser'=>$mid,'uniacid'=>$_SESSION['uniacid']),'mid','mid');
		$canyusnum=0;
		if($downmids){
			$downmids=array_keys($downmids);
			$canyus=pdo_getnorepeat('jy_stpinggou_log',array('status'=>1,'type'=>0,'mid' =>$downmids,'uniacid'=>$_SESSION['uniacid']),'mid');
			$canyusnum=count($canyus);
		}
		return $canyusnum;
	}
	/*
		获取teamid
	*/
	public function getteamid($goodsid,$status=0){
		$params=array(
				'uniacid'=>$_SESSION['uniacid'],
				'goodsid'=>$goodsid,
				'status'=>$status,
				'plugin_name'=>'jy_stpinggou',
		);
		$team=pdo_get('jy_weishop_groups_team',$params,'id');
		if(!$team['id']){
			$insertdata=array(
				'uniacid'=>$_SESSION['uniacid'],
				'goodsid'=>$goodsid,
				'mid'=>$_SESSION['uid'],
				'plugin_name'=>'jy_stpinggou',
				'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_weishop_groups_team',$insertdata);
			$teamid=pdo_insertid();
			return $teamid;
		}else{
			return $team['id'];
		}
	}
}
?>